<%--
  Created by IntelliJ IDEA.
  User: Rui
  Date: 2018/9/28
  Time: 09:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>


<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <meta charset="utf-8">
    <title><jsp:include page="template.jsp"><jsp:param name="name" value="网站名称"/></jsp:include></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=basePath%>resources/lib/layuiAdmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=basePath%>resources/lib/layuiAdmin/style/admin.css" media="all">
    <link rel="stylesheet" href="<%=basePath%>resources/lib/layuiAdmin/style/login.css" media="all">
</head>
<body>
<div class="layui-header" style="background-color: #fff;">
</div>
<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2></h2>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-cellphone" for="mobile"></label>
                <input type="text" name="mobile" id="mobile" lay-verify="phone" required placeholder="手机" class="layui-input" oninput="if(value.length>11)value=value.slice(0,11);">
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="verification"></label>
                        <input type="hidden" id="referenceCode">
                        <input type="text" name="verification" id="verification" lay-verify="required" required placeholder="验证码" class="layui-input" oninput="if(value.length>6)value=value.slice(0,6);">
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <button type="button" class="layui-btn layui-btn-primary layui-btn-fluid" id="action-send-mobile-code">获取验证码</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="mobile"></label>
                <input type="password" name="password" id="password" lay-verify="pass"placeholder="新密码" lay-verType="tips" lay-verify="required" autocomplete="off" id="LAY_password" class="layui-input">
            </div>

            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="setmypass">确认修改</button>
            </div>
        </div>
    </div>
</div>
<script src="<%=basePath%>resources/lib/layuiAdmin/layui/layui.js"></script>
<script src="<%=basePath%>resources/js/jquery.1.12.4.min.js"></script>

<%@include file="../../resources/inc/footer.jsp" %>

</body>
<script>
    layui.config({
        base: '<%=basePath%>resources/lib/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;
        $('#mobile').blur(function(){
            _checkValidUserNameSubmit();
        });
        form.on('submit(setmypass)',function (e) {
            var password = $("#password").val();
            if (!(_hasNumber(password) && _hasSmallChar(password) && _hasBigChar(password))) {
                layer.msg("密码必须包括大小写字母和数字");
                return;
            }
            var verify = false;
            $.ajax({
                url: "<%=basePath%>/manager/sms/verification_verification_code.action",
                type:"GET",
                data:{codeOrder:$("#referenceCode").val(),code:$("#verification").val()},
                async: false,
                dataType: "json",
                success: function(data) {
                    if (data.success) {
                        verify = true;
                    } else {
                        layer.msg(data.message);
                    }
                },
                error: function(error) {
                    layer.msg("系统繁忙，请稍后再试");
                }
            });
            if(verify){
                $.ajax({
                        url: "<%=basePath%>manager/forgetPassword.action",
                    type:"POST",
                    data:{phone:$("#mobile").val(),password:$("#password").val()},
                    dataType:"JSON",
                    success: function(data) {
                        if(data.success){
                            layer.msg("修改成功!即将跳转登陆页")
                            setTimeout(function () {
                                window.location.replace("<%=basePath%>sso/login.action");
                            },1000)

                        }else{
                            layer.msg(data.message)  };

                    },
                    error: function(error) {
                        layer.msg("系统繁忙，请稍后再试");
                    }
                });
            }
        })

    });
    //验证是否含有数字 8.01.31
    function _hasNumber($value) {
        $bool = false;
        $Letters = "1234567890";
        for($i=0;$i<$value.length;$i++) {
            $c = $value.charAt($i);
            if ($Letters.indexOf($c) != -1) {
                $bool = true;
                break;
            }
        }
        return $bool;
    }
    //验证是否含有小写字母 8.01.31
    function _hasSmallChar($value) {
        $bool = false;
        $Letters = "abcdefghijklnmopqrstuvwxyz";
        for($i=0;$i<$value.length;$i++) {
            $c = $value.charAt($i);
            if ($Letters.indexOf($c) != -1){
                $bool = true;
                break;
            }
        }
        return $bool;
    }
    //验证码是否含有大写字母 8.01.31
    function _hasBigChar($value) {
        $bool = false;
        $Letters = "ABCDEFGHIJKLNMOPQRSTUVWXYZ";
        for($i=0;$i<$value.length;$i++) {
            $c = $value.charAt($i);
            if ($Letters.indexOf($c) != -1) {
                $bool = true;
                break;
            }
        }
        return $bool;
    }
    /**
     * 发送验证码
     */
    function verificationCode(){
        var  mobile = $("#mobile").val().toString();
        if (mobile.length == 0) {
            layer.msg("请输入手机号码");
        }else{
            $.ajax({
                type: "GET",
                url: "<%=basePath%>manager/sms/verification_code.action",
                data:{codeDevice:mobile,codeType:1},
                dataType: "json",
                success: function(data) {
                    console.log(data)
                    if(data.success){
                        layer.msg("发送成功，请查收手机短信！");
                        $("#referenceCode").val(data.referenceCode)
                        _pickerCountDown("mobile", "but_send_mobile_code");
                    }else{
                        layer.msg(data.message);
                    }
                },
                error: function(error) {
                    layer.msg("系统繁忙，请稍后再试");
                }
            });
        }
    }

    //倒计时...
    var cd_timer = 59; //1分钟
    function _pickerCountDown(obj_id, attr_f) {
        $obj = $("#action-send-"+obj_id+"-code");
        if (cd_timer == 0) {
            $obj.html("<i>获取验证码</i>");
            $obj.removeClass("layui-btn-primary").addClass("layui-btn").attr("onClick","verificationCode()");
            cd_timer = 60;
        }else {
            if (undefined==cd_timer){
                cd_timer=60;
            }
            $obj.addClass("layui-btn-primary").removeAttr("onClick");

            $obj.html("<i>"+cd_timer+"秒重新发送</i>");
            cd_timer--;
            setTimeout(function() {
                _pickerCountDown(obj_id, attr_f)
            },	1000);
        }
    }

    function _checkValidUserNameSubmit() {
        var phonenumber = $("#mobile").val();
        var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
        if(phonenumber.length == 11){
            if(!myreg.test(phonenumber)){
                layer.msg("手机号码格式不正确,请重新输入!")
                $("#mobile").val("");
            }else{
                $("#action-send-mobile-code").removeClass("layui-btn-primary").addClass("layui-btn").attr("onClick","verificationCode()");
            }
        }else{
            $("#action-send-mobile-code").addClass("layui-btn-primary").removeAttr("onClick");
        }

    }
</script>


</html>
